package com.keeping.aiimagery.controller;

import cn.hutool.core.convert.ConvertException;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.keeping.aiimagery.service.OrdersServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:zfx-t
 * @version:1.0
 */
@RestController

public class ChatGPTController {
    @Autowired
    private OrdersServicesService ordersServicesService;
    /**
     * 设置聊天api接口
     */
    static String chatEndpoint = "https://api.openai.com/v1/chat/completions";
    /**
     * 设置api密钥
     */
    static String apiKey = "Bearer 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";



    /**
     * 前端维系上下文
     * @param dataList
     * @return
     */
    @PostMapping("/chatgpt")
    public String chatGPT(@RequestBody List<HashMap<String,String>> dataList){
        //
            HashMap<String,String> data = dataList.remove(0);

        int charCount = 1;

        System.out.println(charCount);
        String userAccount = data.get("userAccount");
        String  anwser = chat(dataList);
            System.out.println(anwser);

        if(anwser!=null){
            ordersServicesService.cut(userAccount,1,"AIImageGenerate");
            return anwser;
        }
        return null;
    }
    public  static String chat(List<HashMap<String,String>> dataList ){

        Map<String , Object> paramMap = new HashMap<>();
        paramMap.put("model","gpt-3.5-turbo");
        paramMap.put("temperature",0.8);
//        List<Map<String,String>> dataList = new ArrayList<>();
//        dataList.add(new HashMap<String,String>(){{
//            put("role","user");
//            put("content",question);
//        }});
        paramMap.put("messages",dataList);
        JSONObject message = null;
        /**
         * 设置代理
         */
        String proxyHost = "127.0.0.1";
        int proxyPort = 7890;
        Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(proxyHost,proxyPort));
        try{
            String body = HttpRequest.post(chatEndpoint)
                    .setProxy(proxy)
                    .header("Authorization",apiKey)
                    .header("Content-Type","application/json")
                    .body(JSONUtil.toJsonStr(paramMap))
                    .execute()
                    .body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            JSONArray choices = jsonObject.getJSONArray("choices");
            JSONObject result = choices.get(0,JSONObject.class,Boolean.TRUE);
            message = result.getJSONObject("message");
        }catch (HttpException e){
            e.printStackTrace();
        }catch (ConvertException e){
            e.printStackTrace();
        }
//        koufeiService.
        return message.getStr("content");
    }

}
